package ca.uwindsor;

import java.util.HashMap;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ViewTaskList extends ListActivity  {


	ListView lv;
	HashMap<Integer, Integer> idArray = new HashMap<Integer, Integer>(); //stores the id of the table row to access later

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//fullscreen
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
				this, android.R.layout.simple_list_item_multiple_choice);

		// TODO:
		// add ur list to adapter using adapter.add() from database.....
		// test do remove this line use database to populate
		
		//Populate the list with the tasks from the database.
		final MyDBAdapter db = new MyDBAdapter(this);
		db.setDATABASE_TABLE("tasks");
		db.open();
		
		Cursor ret = db.getAllEntries();
		int i = 0;
		if(ret.getCount()>0){
			ret.moveToFirst();
			do{
				adapter.add(ret.getString(1));
				idArray.put(i, ret.getInt(0));//store in array to retrieve later.
				i++;
				ret.moveToNext();
			}while(!ret.isAfterLast());
		}
		
		db.close();
		setListAdapter(adapter);
		lv = getListView();
		lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		
		lv.setTextFilterEnabled(true);
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		switch (id) {
        case 1:
		LayoutInflater inflater =
				(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View layout = 
				inflater.inflate(R.layout.add_tasklist, null);

		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setView(layout);
		alert.setTitle("Add a Task");
		
		
		alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO enter value into database.
				final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
				db.setDATABASE_TABLE("tasks");
				db.open();
				
			    EditText txtDescription = (EditText) layout.findViewById(R.id.task);
			    ContentValues newValues = new ContentValues();
				
				newValues.put("description",txtDescription.getText().toString());
				db.insertEntry(newValues);
				db.close();
				
				//reload task list
				Intent tasklist = new Intent("ca.uwindsor.VIEWTASKLIST");
				startActivity(tasklist);
				
			}

		});
		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO  do nothing...go back to list.
				
			}

		});
		

		AlertDialog addTaskDialog = alert.create();
        return addTaskDialog;
        
        //Remove Dialog
        case 2:
        	final AlertDialog.Builder alert2 = new AlertDialog.Builder(this);
    	
        	alert2.setMessage("Remove selected tasks?");
        	
    		alert2.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

    			public void onClick(DialogInterface dialog, int which) {
    				// TODO enter value into database.
    				final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
    				db.setDATABASE_TABLE("tasks");
    				db.open();
    				SparseBooleanArray checkedItems = lv.getCheckedItemPositions();
    				//remove entries from database.
    				for(int i=0;i<idArray.size();i++){
    	        		if(checkedItems.get(i)){
    	        			db.removeEntry("_id", idArray.get(i));
    	        		}
    	        	}
    	        	
    				db.close();
    			
    				//reload task list
    				Intent tasklist = new Intent("ca.uwindsor.VIEWTASKLIST");
    				startActivity(tasklist);
    				
    			}

    		});
    		alert2.setNegativeButton("No", new DialogInterface.OnClickListener() {

    			public void onClick(DialogInterface dialog, int which) {
    				// TODO  do nothing...go back to list.
    				
    			}

    		});
    		alert2.show();
    		break;
		}
		return null;
	}
	
	
	//for menu do not change
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.task_menu, menu);
		return true;
	}
	
	//for menu do not change
		@Override
		public boolean onOptionsItemSelected(MenuItem item){
			
			switch(item.getItemId())
			{
			case R.id.menuaddtask:
				showDialog(1);
				return true;
				
			case R.id.menudeletetask:
				showDialog(2);
				return true;
				
			case R.id.menumarkall:
				for(int i =0;i<idArray.size();i++){
					lv.setItemChecked(i, true);	
				}
				return true;
				
			case R.id.menumarknone:
				for(int i =0;i<idArray.size();i++){
					lv.setItemChecked(i, false);	
				}
				return true;
			}
			return false;
			
		}

}
